package com.gsm.phimhanhdong;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

public class DownloadFile extends AsyncTask<String, Integer, String> {
	public static final String apk_name= "phimhanhdong.apk";
	public static final String root_path= "/phimhanhdong";
	ProgressDialog downloadUpdateProgress;
	Context context;
	public DownloadFile(Context context){
		this.context = context;
		downloadUpdateProgress = new ProgressDialog(context);
		downloadUpdateProgress.setMessage(context.getResources().getString(
				R.string.downloadnewversion));
		downloadUpdateProgress.setIndeterminate(false);
		downloadUpdateProgress.setMax(100);
		downloadUpdateProgress
				.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		downloadUpdateProgress.setCancelable(false);
	}
	@Override
	protected String doInBackground(String... sUrl) {
		try {
			URL url = new URL(sUrl[0]);
			URLConnection connection = url.openConnection();
			connection.connect();
			// this will be useful so that you can show a typical 0-100%
			// progress bar
			int fileLength = connection.getContentLength();

			// download the file
			InputStream input = new BufferedInputStream(url.openStream());
			File folder = new File(
					Environment.getExternalStorageDirectory()
							+ root_path);
			boolean success = true;
			String filePath = null;
			if (!folder.exists()) {
				success = folder.mkdir();
			}
			if (success) {
				// Do something on success
				filePath = Environment.getExternalStorageDirectory()
						.getAbsolutePath() + root_path+"/" + apk_name;
				Log.e("FilePath", filePath);
			} else {
				downloadUpdateProgress.dismiss();
			}

			OutputStream output = new FileOutputStream(filePath);

			byte data[] = new byte[1024];
			long total = 0;
			int count;
			while ((count = input.read(data)) != -1) {
				total += count;
				// publishing the progress....
				publishProgress((int) (total * 100 / fileLength));
				output.write(data, 0, count);
			}

			output.flush();
			output.close();
			input.close();
		} catch (Exception e) {
			downloadUpdateProgress.dismiss();
		}
		return null;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		downloadUpdateProgress.show();
	}

	@Override
	protected void onProgressUpdate(Integer... progress) {
		super.onProgressUpdate(progress);
		downloadUpdateProgress.setProgress(progress[0]);
		if (progress[0] == 100) {
			File folder = new File(
					Environment.getExternalStorageDirectory()
							+ "/ConGaiXinh");
			downloadUpdateProgress.dismiss();
			// start install latest update
			File fileLocation = new File(folder, apk_name);
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setDataAndType(Uri.fromFile(fileLocation),
					"application/vnd.android.package-archive");
			context.startActivity(intent);
		}
	}
}

